# coding:UTF-8
from django.db import models
from django.contrib import admin
from account.models import UserInfo

class BlogInfoQuerySet(models.QuerySet):
    def sorted_for_newest(self):
        return self.all().order_by('createTime')

    def sorted_for_hotest(self):
        return self.all().order_by('scan_count')

    def sorted_for_classical(self):
        return self.all().order_by('praise_count')

class BlogInfo(models.Model):
    status = models.CharField(max_length=10, default="True")
    blog_title = models.CharField(max_length=100)
    blog_tags = models.CharField(max_length=100)
    blog_author = models.ForeignKey(UserInfo, related_name="blog_list")
    blog_preview = models.CharField(max_length=150)
    blog_content = models.TextField()
    blog_pic_url = models.CharField(max_length=100, null=True, blank=True)
    praise_count = models.IntegerField(default=0)
    scan_count = models.IntegerField(default=0)
    createTime = models.DateTimeField(auto_now_add=True)

    objects = BlogInfoQuerySet.as_manager()

    def __unicode__(self):
        return self.blog_title

    class Meta:
        db_table = 'blog_info'


class BlogInfoAdmin(admin.ModelAdmin):
    list_display = ('blog_title',
                    'status',
                    'blog_tags',
                    'blog_author',
                    'scan_count',
                    'praise_count',
                    'createTime')
    search_fields = ('pk',
                     'blog_title',
                     'blog_tags')

    raw_id_fields = ('blog_author',)
    list_per_page = 20 # 每页显示多少条记录




admin.site.register(BlogInfo, BlogInfoAdmin)
